<?php
/**
 * desc Created by wang.
 * User: wang
 * motto: 是时候为小时候吹过的牛而努力了!
 * Date: 2020/3/17 10:55
 */

namespace app\admin\controller;


use app\BaseController;
use think\exception\HttpResponseException;
use app\admin\business\Auth as BusAuth;
use think\facade\View;

class AdminBase extends BaseController
{
    public $adminUser = null;
    public $myAuth = null;

    public function initialize()
    {
        parent::initialize();
        //检测登录
        if (!$this->isLogin()) {
            return $this->redirect(url('/admin/login/index'), 302);
        }
        define('UID', $this->adminUser['id']);
        define('RID', $this->adminUser['role_id']);

        $controller = strtolower(request()->controller());
        $action = strtolower(request()->action());
        $url = "admin/" . $controller . "/" . $action;

        //跳过检测以及主页权限
        $arrAuth = ['admin/index/index', 'admin/index/home', 'admin/index/edit', 'admin/index/logout'];
        if (UID != 1) {
            //权限检测
            if (!in_array($url, $arrAuth)) {
                if (!BusAuth::check($url, RID)) {
                    if ($this->request->isAjax()) {
                        $arr = [
                            'code' => config('status.error'),
                            'msg' => '无权限'
                        ];
                        echo json_encode($arr);
                        exit();
                    }else{
                        $this->redirect(url('/admin/error/index'));
                        exit;
                    }
                }
            }

        }
        //获取非超级管理员权限
        $this->myAuth = BusAuth::getMyAuth(RID);
        View::assign('myAuth', $this->myAuth);


    }

    /**
     * 功能说明 判断是否登录
     * @author: wang
     * @Date: 2020/3/31 14:34
     * @return bool
     */
    public function isLogin()
    {
        $this->adminUser = session('session_admin');
        if (empty($this->adminUser)) {
            return false;
        }
        return true;
    }

    /**
     * 功能说明 重定向
     * @author: wang
     * @Date: 2020/3/17 10:58
     * @param mixed ...$args
     */
    public function redirect(...$args)
    {
        throw new HttpResponseException(redirect(...$args));
    }


}